{% extends "base.html" %}
{% block pagetitle %}View Request{% endblock %}
{% block content %}
<script type="text/javascript"><!--
function updateRequestStateDone(responseText) {
  var json_return = parseJsonText(responseText);
  if (json_return['status'] != 'OK') {
    alert('operation failed: ' + json_return['message']);
  } else {
    alert('Success');
    document.getElementById('request_state').innerHTML = json_return['state'];
  }
}

function handleApprove(form) {
  ajaxUpdateForm(form, '', 'updateRequestStateDone');
}

function handleSubscribe(form, is_subscribe) {
  document.getElementById('is_subscribe').value = is_subscribe;
  form.submit();
}

//--></script>


<h1>{{nickname}}</h1>

<h2>{{ MSG_CR_ITEMNAME }}:&nbsp;{{ req.item_name }}&nbsp;(<a href="createrequest?qid={{ req.key }}">{{ MSG_CMN_EDIT }})</a></h2>
<div class=art-box>
  <table>
    <tr>
      <td>{{ MSG_VR_CREATOR }}</td>
      <td>{{ creator.key }}</td>
    </tr>
    <tr>
      <td>{{ MSG_CR_ITEMDESC }}</td>
      <td>{{ req.item_description }}</td>
    </tr>
    <tr>
      <td>{{ MSG_CR_COST }}</td>
      <td>{{ req.estimated_price }}</td>
    </tr>
    <tr>
      <td>{{ MSG_CR_SOURCE }}</td>
      <td>{{ req.source }}</td>
    </tr>
    <tr>
      <td>{{ MSG_CR_LOCATION }}</td>
      <td>{{ req.location }}</td>
    </tr>
    <tr>
      <td>{{ MSG_CR_CONTACT }}</td>
      <td>{{ req.contact_info }}</td>
    </tr>
    <tr>
      <td>State:</td>
      <td><p id="request_state">{{ req.state }}</p></td>
    </tr>
  </table>
  <form action="createrequest" method="post" onsubmit="return false;">
    <input type="hidden" name="qid" value="{{ req.key }}"></input>
    <input type="hidden" name="post_action" value="Action.approve"></input>
    <select id="state" name="state">
      <option value="0">PENDING</option>
      <option value="1">OPEN</option>
      <option value="2">FUNDED</option>
      <option value="3">SHIP</option>
      <option value="4">CLOSED</option>
      <option value="5">DELETED</option>
    </select>
    <input type="button" name="approve" value="Change State" onclick="javascript:handleApprove(this.form)"></input>
  </form>
  <br><br>
</div>


<h2>{{ MSG_VR_RELATEDMSG }}&nbsp;<a href="postmessage?qid={{ req.key }}">{{ MSG_VR_CREATEMSG }}</a></h2>
<div class=art-box>
  <table class="art-table">
    <thead>
      <tr>
        <td><b>Author</b></td>
        <td><b>Message</b></td>
        <td><b>Date</b></td>
        <td><b>Delete</b></td>
        <td><b>Edit</b></td>
      </tr>
    </thead>
    <tbody>
      {% for req_msg in req_messages %}
      <tr class="{% cycle row1,row2 %}">
        <td>{{ req_msg.creator.key }}</td>
        <td>{{ req_msg.message }}</td>
        <td>{{ req_msg.date }}</td>
	<td>
	  <form action="postmessage" method="post">
	    <input type="hidden" name="post_action" value="Action.delete"></input>
	    <input type="hidden" name="mid" value="{{ req_msg.key }}"></input>
	    <input type="hidden" name="redirect_url" value="/viewrequest?qid={{ req.key }}"></input>
	    <input type="submit" value="delete"></input>
	  </form>
	</td>
	<td>
	  <a href="/postmessage?mid={{ req_msg.key }}">Edit</a>
	</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

<h2>{{ MSG_VR_PLEDGES }}&nbsp;<a href="/createpledge?qid={{ req.key }}">{{ MSG_VR_CREATEPLEDGE }}</a></h2>
<div class=art-box>
  <table class="art-table">
    <thead>
      <tr>
        {% if is_admin %}
        <td><b>Delete</b></td>
        {% endif %}
        <td><b>Donator</b></td>
        <td><b>Amount</b></td>
        <td><b>Description</b></td>
        <td><b>Detail</b></td>
        <td><b>Edit</b></td>
        <td><b>Delete</b></td>
      </tr>
    </thead>
    <tbody>
      {% for pledge in pledges %}
      <tr class="{% cycle row1,row2 %}">
        {% if is_admin %}
        <td><input type="checkbox" name="delete" value={{person.key }}></td>
        {% endif %}
        <td>{{ pledge.donor_profile.name }}</td>
        <td>{{ pledge.amount }}</td>
        <td>{{ pledge.description }}</td>
        <td><a href="/viewpledge?pid={{ pledge.key }}">Detail</a></td>
        <td><a href="/createpledge?pid={{ pledge.key }}">Edit</a></td>
        <td><a href="/createpledge?pid={{ pledge.key }}">Delete</a></td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

<h2>{{ MSG_VR_ORDERS }}&nbsp;<a href="/createorder?qid={{ req.key }}">{{ MSG_VR_CREATEORDER }}</a></h2>
<div class=art-box>
  <table class="art-table">
    <thead>
      <tr>
        {% if is_admin %}
        <td><b>Delete</b></td>
        {% endif %}
        <td><b>Description</b></td>
        <td><b>Detail</b></td>
        <td><b>Edit</b></td>
        <td><b>Delete</b></td>
      </tr>
    </thead>
    <tbody>
      {% for order in orders %}
      <tr class="{% cycle row1,row2 %}">
        {% if is_admin %}
        <td><input type="checkbox" name="delete" value={{person.key }}></td>
        {% endif %}
        <td>{{ order.description }}</td>
        <td><a href="/vieworder?oid={{ order.key }}">Detail</a></td>
        <td><a href="/createorder?oid={{ order.key }}">Edit</a></td>
        <td><a href="/vieworder?oid={{ order.key }}">Delete</a></td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

<h2>{{ MSG_VR_SUBSCRIBERS }}</h2>
<div class=art-box>
  <table class="art-table">
    <thead>
      <tr>
        <td><b>Email</b></td>
        <td><b>Remove</b></td>
      </tr>
    </thead>
    <tbody>
      {% for one in req.notifiers %}
      <tr class="{% cycle row1,row2 %}">
        <td>{{ one }}</td>
        <td><a href="#">Delete</a></td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>
<form action="viewrequest" method="post">
<input type="hidden" name="post_action" value="Action.subscribe"></input>
<input type="hidden" name="qid" value="{{ req.key }}"></input>
<input type="hidden" id="is_subscribe" name="is_subscribe" value="1"></input>
<input type="button" onclick="handleSubscribe(this.form, 1);" value="{{ MSG_VR_SUBSCRIBE }}"></input>
<input type="button" onclick="handleSubscribe(this.form, 0);" value="{{ MSG_VR_UNSUBSCRIBE }}"></input>
</form>
{% endblock %}
